package com.wwp.tbshop.presenter;

import android.content.Context;
import android.widget.Toast;

import com.wwp.tbshop.activity.AddOrEditAddressActivity;
import com.wwp.tbshop.activity.AddressManageActivity;
import com.wwp.tbshop.model.bean.BaseBean;

import retrofit2.Call;

/**
 * Created by Xiaokun on 2017/6/16.
 */

public class AddressChangePresenter extends BasePresenter{


    private Context mContext;

    public AddressChangePresenter(Context context){
        this.mContext=context;
    }


    public void setAddressDefault(int id, int userid){
        Call<BaseBean> addressList = mApiService.setAddressDefault(id,userid);
        addressList.enqueue(mCallback);
    }

    public void deleteAddress(int id, int userid){
        Call<BaseBean> addressList = mApiService.deleteAddress(id,userid);
        addressList.enqueue(mCallback);
    }

    public void saveAddress(String id, String name, String phoneNumber, String province, String city, String addressArea, String addressDetail, String zipCode, int isDefault, int userid){
        Call<BaseBean> addressList = mApiService.saveAddress(id,name,phoneNumber,province,city,addressArea,addressDetail,zipCode,isDefault,userid);
        addressList.enqueue(mCallback);
    }


    @Override
    protected void onServerBug(int code) {

    }

    @Override
    protected void onConnectError(String message) {
        Toast.makeText(mContext,"网络错误，修改失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onSuccess(BaseBean baseBean) {
        String error_code = baseBean.getError_code();
        if (error_code!=null){
            Toast.makeText(mContext,"网络出错",Toast.LENGTH_SHORT).show();
        }else {
            switch (baseBean.getResponse()){
                case "addressDefault":
                    Toast.makeText(mContext,"默认成功",Toast.LENGTH_SHORT).show();
                    ((AddressManageActivity) mContext).updateSuccess();
                    break;
                case "addressDelete":
                    Toast.makeText(mContext,"删除成功",Toast.LENGTH_SHORT).show();
                    ((AddressManageActivity) mContext).updateSuccess();
                    break;
                case "addresssave":
                    Toast.makeText(mContext,"添加修改成功",Toast.LENGTH_SHORT).show();
                    ((AddressManageActivity) mContext).updateSuccess();
                    break;
            }


        }
    }

}
